package com.longcai.controller.clyjheadlines;


import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.homepage.ClyjHeadlines;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.clyjheadlines.ClyjHeadlinesService;
import com.longcai.util.OSSUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 平台头条 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-03-27
 */
@RestController
@RequestMapping("/clyjHeadlines")
public class ClyjHeadlinesController {

    @Autowired
    private ClyjHeadlinesService clyjHeadlinesService;

    /**
     * 获取头条列表
     *
     * @param clyjHeadlines
     * @param pageHelper
     * @return
     */
    @ApiOperation(value = "头条列表")
    @GetMapping(value = "/headList")
    public PageTableResponse headList(ClyjHeadlines clyjHeadlines, PageHelper pageHelper) {
        return this.clyjHeadlinesService.headList(clyjHeadlines, pageHelper);
    }

    /**
     * 新增
     *
     * @param clyjHeadlines
     * @param icon
     * @return
     */
    @PostMapping(value = "/saveHead")
    @ApiOperation(value = "新增头条")
    @PreAuthorize("hasAuthority('headline:add')")
    public ClyjHeadlines saveHead(ClyjHeadlines clyjHeadlines, MultipartFile icon) {
        ClyjHeadlines u = this.clyjHeadlinesService.getTitle(clyjHeadlines.getTitle());
        if (u != null) {
            throw new IllegalArgumentException(clyjHeadlines.getTitle() + "已存在");
        }
        if (icon != null) {
            //文档上传oss
            try {
                String fileName = icon.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = icon.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/HEAD/");
                clyjHeadlines.setPics(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
                e.getMessage();
            }
        }
        return clyjHeadlinesService.saveHead(clyjHeadlines);
    }

    /**
     * 根据id获取头条信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取头条信息")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('headline:query')")
    public ApiResult user(@PathVariable String id) {

        return this.clyjHeadlinesService.getById(id);
    }

    /**
     * 修改头条信息
     *
     * @param clyjHeadlines
     * @return
     */
    @PostMapping(value = "updateHeadline")
    @ApiOperation(value = "修改用户")
    @PreAuthorize("hasAuthority('headline:update')")
    public Integer updateHeadline(ClyjHeadlines clyjHeadlines, MultipartFile icon) {
        if (icon != null) {
            //文档上传oss
            try {
                String fileName = icon.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = icon.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/HEAD/");
                clyjHeadlines.setPics(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return this.clyjHeadlinesService.updateHead(clyjHeadlines);
    }

    /**
     * 删除头条
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除头条")
    @PreAuthorize("hasAuthority('headline:delete')")
    public void delete(@PathVariable String id) {
        this.clyjHeadlinesService.deleteById(id);
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping(value = "uploadFile")
    @ApiOperation(value = "上传文件")
    @ResponseBody
    public Map<String, Object> uploadFile(MultipartFile file) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (file != null) {
            //文档上传oss
            try {
                String fileName = file.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = file.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/CONTEXT/");
                resultMap.put("code", 0);
                uploadByInputStream.put("src", uploadByInputStream.get("path"));
                resultMap.put("data", uploadByInputStream);
                return resultMap;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return resultMap;
    }

}

